
#include "SWidget/SConfigureDetailsView.h"
#include "Modules/ModuleManager.h"
#include "PropertyEditorModule.h"
#include "Modules/ModuleManager.h"


#define LOCTEXT_NAMESPACE "WidgetGroupConfigureDetailsView"

void SConfigureDetailsView::Construct(const FArguments& InArgs)
{
	CreateDetailView();

	ChildSlot
		[
			SettingsView->GetWidget()->AsShared()
		];

	NameChanged = InArgs._NameChanged;
}

void SConfigureDetailsView::CreateDetailView()
{
	ConfigureSettingBase = MakeShareable(new FInputConfigureGroupData);
	FPropertyEditorModule& EditModule = FModuleManager::Get().GetModuleChecked<FPropertyEditorModule>("PropertyEditor");

	FDetailsViewArgs DetailsViewArgs;
	{
		DetailsViewArgs.bAllowSearch = true;
		DetailsViewArgs.bHideSelectionTip = true;
		DetailsViewArgs.bLockable = false;
		DetailsViewArgs.bSearchInitialKeyFocus = true;
		DetailsViewArgs.bUpdatesFromSelection = false;
		DetailsViewArgs.NotifyHook = nullptr;
		DetailsViewArgs.bShowOptions = true;
		DetailsViewArgs.bShowModifiedPropertiesOption = false;
		DetailsViewArgs.bShowScrollBar = false;
	}

	FStructureDetailsViewArgs StructureViewArgs;
	{
		StructureViewArgs.bShowObjects = true;
		StructureViewArgs.bShowAssets = true;
		StructureViewArgs.bShowClasses = true;
		StructureViewArgs.bShowInterfaces = true;
	}

	SettingsView = EditModule.CreateStructureDetailView(DetailsViewArgs, StructureViewArgs, nullptr);
	FStructOnScope* Struct = new FStructOnScope(FInputConfigureGroupData::StaticStruct(), (uint8*)ConfigureSettingBase.Get());
	SettingsView->SetStructureData(MakeShareable(Struct));
	SettingsView->GetOnFinishedChangingPropertiesDelegate().AddRaw(this, &SConfigureDetailsView::ValueChangedEvent);
}

void SConfigureDetailsView::ValueChangedEvent(const FPropertyChangedEvent& pce)
{
	if (pce.GetPropertyName() == TEXT("GroupKey"))
	{
		NameChanged.ExecuteIfBound(ConfigureSettingBase->GroupKey);
	}
	else if (InputConfigureLibraryEditor::bAutoSave == true)
	{
		InputConfigureLibraryEditor::InputConfigFile_Save(ConfigureSettingBase);
	}
}


void SConfigureDetailsView::UpdateDetailView(FString inGroupKey)
{
	ConfigureSettingBase->LoadDataAtConfigFile(inGroupKey);
}

void SConfigureDetailsView::ResetGroupKey(FString inGroupKey)
{
	ConfigureSettingBase->GroupKey = inGroupKey;
}



FString SConfigureDetailsView::GetGroupKey()
{
	return ConfigureSettingBase->GroupKey;
}

TSharedPtr<FInputConfigureGroupData> SConfigureDetailsView::GetGroup()
{
	return ConfigureSettingBase;
}

#undef LOCTEXT_NAMESPACE

